﻿Imports DotNetNuke
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class ActiveForumViewerSettings
        Inherits Entities.Modules.ModuleSettingsBase

        Public Overrides Sub LoadSettings()
            Try
                If Not Page.IsPostBack Then
                    ' Load settings from TabModuleSettings: specific to this instance
                    'Dim setting1 As String = CType(TabModuleSettings("settingname1"), String)
                    LoadForums()
                    ' Load settings from ModuleSettings: general for all instances
                    If Not CType(Settings("AFForumModuleID"), String) Is Nothing Then
                        drpForumInstance.SelectedIndex = drpForumInstance.Items.IndexOf(drpForumInstance.Items.FindByValue(CType(Settings("AFForumModuleID"), String)))
                        LoadForumGroups(CType(Settings("AFForumModuleID"), Integer))
                    End If
                    If Not CType(Settings("AFTopicsTemplate"), String) Is Nothing Then
                        BindTemplates(CType(Settings("AFForumModuleID"), Integer))
                        drpTopicsTemplate.SelectedIndex = drpTopicsTemplate.Items.IndexOf(drpTopicsTemplate.Items.FindByValue(CType(Settings("AFTopicsTemplate"), String)))
                    End If
                    If Not CType(Settings("AFForumViewTemplate"), String) Is Nothing Then
                        drpForumViewTemplate.SelectedIndex = drpForumViewTemplate.Items.IndexOf(drpForumViewTemplate.Items.FindByValue(CType(Settings("AFForumViewTemplate"), String)))
                    End If
                    If Not CType(Settings("AFTopicTemplate"), String) Is Nothing Then
                        drpTopicTemplate.SelectedIndex = drpTopicTemplate.Items.IndexOf(drpTopicTemplate.Items.FindByValue(CType(Settings("AFTopicTemplate"), String)))
                    End If
                    If Not CType(Settings("AFForumGroup"), String) Is Nothing Then
                        drpForum.SelectedIndex = drpForum.Items.IndexOf(drpForum.Items.FindByValue(CType(Settings("AFForumGroup"), String)))
                    End If
                    'If Not CType(Settings("AFEnableToolbar"), String) Is Nothing Then
                    '    chkEnableToolbar.Checked = CType(Settings("AFEnableToolbar"), Boolean)
                    'End If
                End If
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Public Overrides Sub UpdateSettings()
            Try
                Dim objModules As New Entities.Modules.ModuleController
                ' Update ModuleSettings
                objModules.UpdateModuleSetting(ModuleId, "AFTopicsTemplate", drpTopicsTemplate.SelectedItem.Value)
                objModules.UpdateModuleSetting(ModuleId, "AFTopicTemplate", drpTopicTemplate.SelectedItem.Value)
                objModules.UpdateModuleSetting(ModuleId, "AFForumViewTemplate", drpForumViewTemplate.SelectedItem.Value)
                objModules.UpdateModuleSetting(ModuleId, "AFForumModuleID", drpForumInstance.SelectedItem.Value)
                objModules.UpdateModuleSetting(ModuleId, "AFForumGroup", drpForum.SelectedItem.Value)
                'objModules.UpdateModuleSetting(ModuleId, "AFEnableToolbar", CType(chkEnableToolbar.Checked, String))
                Dim ForumGroup As String = ""
                Dim ForumGroupID As Integer = 0
                ForumGroup = drpForum.SelectedItem.Value
                If InStr(ForumGroup, "GROUPID:") > 0 Then
                    objModules.UpdateModuleSetting(ModuleId, "AFViewType", "AFGROUP")
                Else
                    objModules.UpdateModuleSetting(ModuleId, "AFViewType", "TOPICS")
                End If
                Dim int As Integer = ForumGroup.IndexOf(":") + 1
                Dim sID As String = ForumGroup.Substring(int)
                'ForumGroupID = CType(ForumGroup.Substring(ForumGroup.IndexOf(":")), Integer)
                objModules.UpdateModuleSetting(ModuleId, "AFForumGroupID", sID)
                DataCache.CacheClear(drpForumInstance.SelectedItem.Value & TabId & sID & "TopicTemplate")
                DataCache.CacheClear(drpForumInstance.SelectedItem.Value & TabId & sID & "TopicsTemplate")
                DataCache.CacheClear(drpForumInstance.SelectedItem.Value & TabId & "ForumTemplate")
                ' Redirect back to the portal home page
                Response.Redirect(Utilities.NavigateUrl(TabId), True)
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Public Sub LoadForums()
            'Dim dr As IDataReader
            'dr = DataProvider.Instance.PortalForums(PortalId)
            'Dim i As Integer = 0
            'While dr.Read
            '    drpForumInstance.Items.Insert(i, New Web.UI.WebControls.ListItem(CType(dr("TabName"), String), CType(dr("ModuleID"), String)))
            'End While
            'dr.Close()
            Dim i As Integer = 0
            Dim mc As New DotNetNuke.Entities.Modules.ModuleController
            Dim tc As New DotNetNuke.Entities.Tabs.TabController
            Dim ti As DotNetNuke.Entities.Tabs.TabInfo = Nothing
            For Each mi As DotNetNuke.Entities.Modules.ModuleInfo In mc.GetModules(PortalId)
                If mi.DesktopModule.ModuleName.Trim.ToLowerInvariant = "Active Forums".ToLowerInvariant And mi.IsDeleted = False Then
                    ti = tc.GetTab(mi.TabID, PortalId, False)
                    If Not ti Is Nothing Then
                        If ti.IsDeleted = False Then
                            drpForumInstance.Items.Insert(i, New ListItem(ti.TabName & " - " & mi.DesktopModule.ModuleName, CType(mi.ModuleID, String)))
                            i += 1
                        End If
                    End If

                End If
            Next

            drpForumInstance.Items.Insert(0, New ListItem("-- Select a Forum Instance --", "-1"))
        End Sub
        Public Sub LoadForumGroups(ByVal ForumModuleID As Integer)
            drpForum.Items.Insert(0, New ListItem("-- Select a Group or Forum --", "-1"))
            Dim dr As IDataReader = DataProvider.Instance.Forums_List(PortalId, ForumModuleID, -1, -1, False)
            Dim i As Integer = 1
            Dim GroupName As String = String.Empty
            Dim ForumName As String = String.Empty
            Dim ForumID As Integer = -1
            While dr.Read
                If GroupName <> CType(dr("GroupName"), String) Then
                    drpForum.Items.Insert(i, New ListItem(CType(dr("GroupName"), String), "GROUPID:" & CType(dr("ForumGroupID"), String)))
                    i += 1
                    GroupName = CType(dr("GroupName"), String)
                End If


                If ForumID <> CType(dr("ForumID"), Integer) Then
                    drpForum.Items.Insert(i, New ListItem("|---" & CType(dr("ForumName"), String), "FORUMID:" & CType(dr("ForumID"), String)))
                    i += 1
                    ForumID = CType(dr("ForumID"), Integer)
                End If


            End While
            dr.Close()

        End Sub
        Private Sub BindTemplates(ByVal ForumModuleID As Integer)
            Dim sDefault As String = Utilities.GetSharedResource("[RESX:Default]", True)
            BindTemplateDropDown(ForumModuleID, drpTopicsTemplate, Templates.TemplateTypes.TopicsView, sDefault, "0")
            BindTemplateDropDown(ForumModuleID, drpTopicTemplate, Templates.TemplateTypes.TopicView, sDefault, "0")
            BindTemplateDropDown(ForumModuleID, drpForumViewTemplate, Templates.TemplateTypes.ForumView, sDefault, "0")
        End Sub
        Private Sub drpForumInstance_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpForumInstance.SelectedIndexChanged
            BindTemplates(CType(drpForumInstance.SelectedItem.Value, Integer))
            LoadForumGroups(CType(drpForumInstance.SelectedItem.Value, Integer))
        End Sub
        Public Sub BindTemplateDropDown(ByVal ForumModuleId As Integer, ByVal drp As DropDownList, ByVal TemplateType As Templates.TemplateTypes, ByVal DefaultText As String, ByVal DefaultValue As String)
            Dim tc As New TemplateController
            drp.DataTextField = "Title"
            drp.DataValueField = "TemplateID"
            drp.DataSource = tc.Template_List(PortalId, ForumModuleID, TemplateType)
            drp.DataBind()
            drp.Items.Insert(0, New ListItem(DefaultText, DefaultValue))
        End Sub
    End Class
End Namespace